<template>
	<view class="contribution">
		<view class="line"></view>
		<view class="view1">
			<image :src="contributors.userHeaderImage" mode="aspectFill"></image>
			<text>{{contributors.userNickname}}</text>
		</view>
		<view class="view2">
			<textarea placeholder="分享新鲜事..." maxlength="50" v-model="desc" @blur='bindTextAreaBlur' @input="ordCountChange" />
			<text>{{desc.length}}/50</text>
			</view>
			<view class="view3">
				<image src="../../static/fb39739fd92dd0609d32c62ee2d4dc2.png" mode="aspectFill" @click="ploadPictures"></image>
			</view>
			<view class="view4">
				<text>{{imgArr.length}}/10</text>
				<view class="pictureGroup">
					<image :src="item" mode="aspectFill" v-for="item in imgArr" @click="previewImg(item)"></image>
				</view>
			</view>
			<view class="view5">
				<button type="default" @click="contributeImmediately">立即投稿</button>	
			</view>	
		</view>
	</view>
</template>
<script>
	var _self;
	export default {
		data() {
			return {
				dynamicDescribe:'',
				imgArr: [],
				contributors:{
					userHeaderImage:'',
					userNickname:'',
				},
				remnant:null,
				desc:'',
				src:""
			};
		},
		
		onLoad() {
			this.getToken()
		},
		methods: {
			// 异步获取token
			getToken() {
				try {
					const token = uni.getStorageSync('token');
					if (token) {
						console.log(token)
						this.contribution1(token)
						this.contributeImmediately(token)
					}
				} catch (e) {}
			},
				//投稿用户信息接口
			contribution1(token) {
				this.$request(
					"/comic-time/userInformation", {
					},
					'GET', {
						"Authorization":token
					}
				).then(res => {
					console.log(res)
					this.contributors = res.data
				})
			},
			//上传图片
			 // ploadPictures(){
			 //                let that=this;
			 //                const token = uni.getStorageSync('token');
			 //               uni.chooseImage({
			 //                   count:1, //默认9
			 //                   sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			 //                   sourceType: ['album'], //从相册选择
			 //                   success: function (res) {
			 //                        that.src=res.tempFilePaths[0],
				// 					console.log(res.tempFilePaths[0])
			 //                         uni.uploadFile({
			 //                                   url: 'https://products.rimiedu.com:8085/comic-time/personal/editHeader', 
			 //                                   filePath: res.tempFilePaths[0],
			 //                                   name: 'file',
			 //                                   formData: {
			 //                                       // "host": '192.168.7.7',
			 //                                       // "port":"8080"
			 //                                   },
			 //                                    header: {
			 //                                           'Authorization': token
			 //                                    },
			 //                                   success: (uploadFileRes) => {
			 //                                       console.log(uploadFileRes);
			 //                                   }
			 //                               });
			 //                   }
			 //               });
			 //            },
			ploadPictures() {
				
				let that=this;
				const token = uni.getStorageSync('token');
				uni.chooseImage({
					count: 10,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						console.log(res)
						that.imgArr.push(...res.tempFilePaths)
						console.log(that.imgArr)
						  uni.uploadFile({
						    url: 'https://products.rimiedu.com:8085/comic-time/putImage', 
							// files:that.imgArr,
							filePath: res.tempFilePaths[0],
							name: 'file',
						     formData: {
						     },
						       header: {
						       'Authorization': token
						        },
						      success: (uploadFileRes) => {
						        console.log(uploadFileRes);
						       }
						   });
						                   
					}
				})
			},
			//图片预览
			previewImg(current){
				console.log(current)
				uni.previewImage({
					current,
					urls:this.imgArr
				})
			},
			//立即投稿
			contributeImmediately(){
				// uni.switchTab({
				// 	url:'./contribute'
				// })
				const token = uni.getStorageSync('token');
				//立即投稿接口
				this.$request(
					"/comic-time/addDynamic", {	
						"dynamicDescribe":this.dynamicDescribe,
						"dynamicImage":this.imgArr
					},
					'POST', {
						"Authorization": token
					}
				).then(res => {
					console.log(res)
				})
			},
			//多行文本域失去焦点时触发
			bindTextAreaBlur(e){
				console.log(e.detail.value)
				this.dynamicDescribe=e.detail.value
			},
			//监听字数变化
			ordCountChange(e){
				this.remnant = e.detail.value.length;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.contribution {
		.line {
			width: 750rpx;
			height: 10rpx;
			background-color: #eeeeee;
			margin-top: 26rpx;
		}
		.view1{
			display: flex;
			align-items: center;
			margin-top: 20rpx;
			padding: 0 30rpx;
			// border: 1px solid red;
			image{
				width: 50rpx;
				height: 50rpx;
				border-radius: 50rpx;
				margin-right: 10rpx;
			}
			text{				
					font-family: PingFangSC-Regular;
					font-size: 32rpx;
					font-weight: normal;
					color: #000000;
			}
		}
		.view2{
			padding: 0 30rpx;
			margin-top: 33rpx;
			border-bottom: 1px solid #eeeeee;
			textarea{
				width: 100%;
				height: 202rpx;
			}
			text{
				display: block;
				display: flex;
				justify-content: flex-end;
				font-size: 24rpx;	
				font-family: PingFangSC-Regular;
				color: #666666;
				margin-bottom: 10rpx;
			}
		}
		.view3{
			border-bottom: 1px solid #eeeeee;
				padding: 30rpx 30rpx 30rpx;
			image{
				width: 200rpx;
				height: 200rpx;
	
			}
		}
		.view4{
			padding: 9rpx 30rpx 0;
			text{
				display: block;
				display: flex;
				justify-content: flex-end;
				font-size: 24rpx;	
				font-family: PingFangSC-Regular;
				color: #666666;
			}
			.pictureGroup {
				display: flex;
				flex-flow: row wrap;
				justify-content: flex-start;
				image {
					width: 221rpx;
					height: 221rpx;
					border-radius: 10rpx;
					border: solid 1rpx #e5e5e5;
					margin-bottom: 20rpx;
					margin-right: 10rpx;
				}
			}
		}
		.view5{;
			button{
				    width: 520rpx;
					height: 90rpx;
					background-color: #ff6262;
					border-radius: 45rpx;
					color: white;
					position: fixed;
					left: 0;
					right: 0;
					bottom:50rpx;					
			      }
	         }
	}
</style>
